import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:travel/model/message.dart';
import 'package:travel/screen/flutter_screenutil.dart';
import 'package:travel/ui/scroll_refresh_config.dart';
import 'package:travel/utils/colors.dart';
import 'package:travel/utils/styles.dart';
import 'package:travel/utils/tools.dart';

class SystemController extends GetxController {
  RefreshController refreshController = RefreshController();

  RxList<MessageSystemModel> pageList = RxList([]);

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 5) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    pageList
        .add(MessageSystemModel(timeStr: '3天前', content: '你提交的个人信息修改申请审核已通过。你本月还有修改资料次数：用户名4次'));

    /// 完毕
    refreshController.loadComplete();
  }

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
  }

  void initData() {
    pageList.add(
        MessageSystemModel(timeStr: '12-25 17:52', content: '你的帐号在一台新设备上成功登录。如非本人操作，请及时修改密码。'));
    pageList
        .add(MessageSystemModel(timeStr: '2天前', content: '你提交的个人信息修改申请审核已通过。你本月还有修改资料次数：用户名0次'));
  }
}

class SystemView extends GetView<SystemController> {
  const SystemView({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF7f7),
          appBar: AppBar(
            automaticallyImplyLeading: true,
            backgroundColor: Colors.white,
            leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Icon(
                Icons.arrow_back_ios,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.color3333),
                size: 20.w,
              ),
            ),
            leadingWidth: 40.w,
            title: Text(
              '系统消息',
              style: StylesUtils.customTextStyle(fontSize: 18.sp, color: ColorsUtils.color0000),
            ),
          ),
          body: scrollRefreshConfig(context),
        ));
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, index) {
        var row = controller.pageList[index];
        return Column(
          children: [
            Container(
              height: 50.h,
              alignment: Alignment.center,
              child: Text(
                row.timeStr,
                style: StylesUtils.customTextStyle(color: ColorsUtils.color9999, fontSize: 12.sp),
              ),
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CircleAvatar(
                  radius: 20.w,
                  backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.color3478),
                  child: ToolsUtils.assetImageSvg('u2521.svg', width: 18.w, height: 20.h),
                ),
                Expanded(
                    child: Container(
                  padding: EdgeInsets.only(left: 15.w),
                  alignment: Alignment.topLeft,
                  child: Container(
                    width: 245.w,
                    padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 15.w),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(
                            topRight: Radius.circular(15.w),
                            bottomRight: Radius.circular(15.w),
                            bottomLeft: Radius.circular(15.w))),
                    child: Text(
                      row.content,
                      style: StylesUtils.customTextStyle(
                          fontSize: 16.sp, color: ColorsUtils.color3333, lineHeight: 1.2.h),
                    ),
                  ),
                ))
              ],
            ),
          ],
        );
      },
      itemCount: controller.pageList.length,
      padding: EdgeInsets.symmetric(horizontal: 15.w),
    );
  }
}
